/*
    5. Write a program in C to insert a new node at the end of a Singly Linked List. 
    Test Data and Expected Output :

    Input the number of nodes : 3                                                                                
    Input data for node 1 : 5                                                                                    
    Input data for node 2 : 6                                                                                    
    Input data for node 3 : 7                                                                                    
                                                                                                                
    Data entered in the list are :                                                                               
    Data = 5                                                                                                     
    Data = 6                                                                                                     
    Data = 7                                                                                                     
                                                                                                                
    Input data to insert at the end of the list : 8                                                              
                                                                                                                
    Data, after inserted in the list are :                                                                       
    Data = 5                                                                                                     
    Data = 6                                                                                                     
    Data = 7                                                                                                     
    Data = 8
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct node* List;
typedef struct node{
    int x;
    List next;
} Node;

List createList(int n)
{
    List root = NULL;
    List newnode = NULL;
    List curNode = NULL;
    int i;
    for(i = 0; i < n; i++){
        newnode = (List)malloc(sizeof(Node));
        if(newnode == NULL)
            return NULL;
        newnode->next = NULL;
        printf("Input the data of node %d : ", i);
        scanf("%d", &(newnode->x));
        
        if(root == NULL){           
            root = newnode;
            curNode = root;
        }else{
            curNode->next = newnode;
            curNode = newnode;
        }
    }

    return root;
}

List insertNodeAtEnd(List root, int x)
{
    List endNode = root;
    List newNode = (List)malloc(sizeof(Node));
    newNode->x = x;
    newNode->next = NULL;
    while(endNode->next != NULL){
        endNode = endNode->next;
    }
    endNode->next = newNode;
    return root;
}

void printList(List list)
{
    if(list == NULL){
        printf("List is empty!\n");
        return;
    }
    for(; list != NULL; list = list->next){
        printf("Data = %d\n", list->x);
    }
}

int main(void)
{
    List root = NULL;
    int n,x;
    printf("Input the number of nodes : ");
    scanf("%d", &n);
    root = createList(n);
    printf("\nData entered in the list are : \n");
    printList(root);
    printf("\nInput data to insert at the end of the list : ");
    scanf("%d", &x);
    root = insertNodeAtEnd(root, x);
    printList(root);

    return 0;

}